<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card style="margin-top: 15px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <template>
        <el-table
          :data="goodsList"
          stripe
          style="width: 100%; margin-top: 15px"
          border
        >
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称">
          </el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格(元)"
            width="150px"
          >
          </el-table-column>
          <el-table-column prop="goods_number" label="商品数量" width="150px">
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="150px">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间" width="180px">
            <template slot-scope="scope">
              {{ scope.row.add_time | dataFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="220px">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="editDialog(scope.row.goods_id)"
                >编辑</el-button
              >
              <el-button
                type="warning"
                size="mini"
                @click="removeById(scope.row.goods_id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      title="修改商品数据"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form :model="editForm" ref="editFormRef" label-width="100px">
        <el-form-item label="商品名称 " prop="goods_name">
          <el-input v-model="editForm.goods_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEditData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 查询参数列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodsList: [],
      // 总数居条数
      total: 0,
      addDialogVisible: false,
      // 保存商品数据库
      addForm: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: ''
      },
      addFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称！', trigger: 'blur' },
          {
            min: 1,
            message: '商品名称不允许为空！',
            trigger: 'blur'
          }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格！', trigger: 'blur' },
          {
            min: 1,
            message: '商品价格不允许为空！',
            trigger: 'blur'
          }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量！', trigger: 'blur' },
          {
            min: 1,
            message: '商品数量不允许为空！',
            trigger: 'blur'
          }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量！', trigger: 'blur' },
          {
            min: 1,
            message: '商品重量不允许为空！',
            trigger: 'blur'
          }
        ]
      },
      editDialogVisible: false,
      editForm: {}
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }

      this.goodsList = res.data.goods
      // console.log(res.data)
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    async editDialog(id) {
      this.editDialogVisible = true
      const { data: res } = await this.$http.get('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败！')
      }

      this.editForm = res.data
    },

    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    // 提交数据到后台
    saveEditData() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return false
        const { data: res } = await this.$http.put(
          'goods/' + this.editForm.goods_id,
          {
            goods_name: this.editForm.goods_name,
            goods_price: this.editForm.goods_price,
            goods_number: this.editForm.goods_number,
            goods_weight: this.editForm.goods_weight
          }
        )
        console.log(res)
        if (res.meta.status !== 201) {
          return this.$message.error('提交商品数据失败')
        }

        this.editDialogVisible = false
        this.getGoodsList()
        this.$message.success('修改商品数据成功')
      })
    },
    async removeById(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('该商品删除失败！')
      }

      this.$message.success('该商品删除成功！')
      this.getGoodsList()
    },
    goAddpage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped></style>
